<?php
/**
 * Created by PhpStorm.
 * User: denh
 * Date: 16/12/20
 * Time: 上午11:27
 */
require_once ADMIN_PATH . '../bs/wxNews/Task.php';

class NewsController extends BaseController {

    function __construct($m = DEFAULT_CONTROLLER, $a = DEFAULT_METHOD) {
        parent::__construct($m, $a);
        $this->tourist_login();
    }
    function index() {
        $do = new DoNews_pictext_template();
        $list = $do->orderby("id desc")->rows();
        require_once $this->template;
    }

    function edit() {
        if ($_SERVER['REQUEST_METHOD'] == "POST") {
            $this->edit_post();
        } else {
            $id = $this->request('id', '');
            $do = new DoNews_pictext_template();
            if ($id) {
                $do->loadById($id);
            }
            require_once $this->template;
        }
    }

    function edit_post() {
        $id = $this->request('id', '');
        $pt_title = $this->request('pt_title', '', true);
        $pt_desc = $this->request('pt_desc', '');
        $pt_pic = $this->request('pt_pic', '', true);
        $template_first = $this->request('template_first', '');
        $template_keyword1 = $this->request('template_keyword1', ''); //消息类别
        $template_keyword2 = $this->request('template_keyword2', ''); //发布人
        $template_keyword3 = $this->request('template_keyword3', ''); //发布时间
        $template_remark = $this->request('template_remark', '');
        $url = $this->request('url', '', true);

        $isForAllUser = $this->request('isForAllUser', '1');
        $userType = $this->request('userType', '');
        $title = $this->request('title', '');
        $hospitalLevel = $this->request('hospitalLevel', '');
        $noExpert = $this->request('noExpert', ''); //不给专家发送
        $onlyPass = $this->request('onlyPass', ''); //只发给通过认证的
        $province = $this->request('province', '');
        $city = $this->request('city', '');

        $note = "y.openID != ''";
        if ($onlyPass) {
            $note .= " and y.状态 = '通过'";
        }
        if($userType) {
            if (count($userType) == 1) {
                $note .= " and y.userType = '$userType[0]'";
            } elseif (count($userType) > 1) {
                $userType = implode("','", $userType);
                $note .= " and y.userType in ('$userType')";
            }
        }
        if ($title) {
            if (count($title) == 1) {
                $note .= " and y.职务 = '$title[0]'";
            } elseif (count($title) > 1) {
                $title = implode("','", $title);
                $note .= " and y.职务 in ('$title')";
            }

        }
        if ($hospitalLevel) {
            if (count($hospitalLevel) == 1) {
                $note .= " and h.level = '$hospitalLevel[0]'";
            } elseif (count($hospitalLevel) > 1) {
                $hospitalLevel = implode("','", $hospitalLevel);
                $note .= " and h.level in ('$hospitalLevel')";
            }
        }
        if ($noExpert) {
            $note .= " and y.用户id not in (select id from `专家`)";
        }
        if ($province) {
            $note .= " and u.province = '$province'";
        }
        if ($city) {
            $note .= " and u.city = '$city'";
        }

        $do = new DoNews_pictext_template();
        $do->set_pt_title($pt_title);
        $do->set_pt_desc($pt_desc);
        $do->set_pt_pic($pt_pic);
        $do->set_template_first($template_first);
        $do->set_template_keyword1($template_keyword1);
        $do->set_template_keyword2($template_keyword2);
        $do->set_template_keyword3($template_keyword3);
        $do->set_template_remark($template_remark);
        $do->set_url($url);
        $do->set_type('图文作业');
        $do->set_isForAllUser($isForAllUser);
        $do->set_note($note);
        $do->set_id($id);
        if ($do->save()) {
            $this->redirect('news', 'index');
        } else {
            $this->errorPage('提交失败');
        }
    }

    function send() {
        $id = $this->request('id', '', true);
        $openId = $this->request('oid', '');
        $phone = $this->request('phone', '');

        $task =  new \bs\wxNews\Task('task', $id);
        $doUser = new Do用户();
        if ($openId) {
            $doUser->set_openID($openId);
            if (!$doUser->where($doUser->data)->row()) {
                $this->jsonOutPut(-1, 'openId不正确');
            }
            $task->sendNews($doUser->data);
        } else if ($phone) {
            $doUser->set_telephone($phone);
            if (!$doUser->where($doUser->data)->row()) {
                $this->jsonOutPut(-1, '手机号不正确');
            }
            $task->sendNews($doUser->data);
        } else {
            $this->run_cli('task_msg', $id);
        }
        $this->jsonOutPut(0, 'ok');
    }

    function run_cli($type, $id) {

        $doNews = new DoNews_pictext_template();
        $doNews->loadById($id);
        $where = $doNews->get_note();

        if ($doNews->get_isForAllUser() == '1') {
            parent::run_cli($type, $id);
        } else {
            $sql = "select count(*) from 医师详情 y
                left join 用户 u on y.用户id = u.id
                left join hospital h on y.医院 = h.name
                where 1=1 and $where";
            $data = pdo()->selectOne($sql);
            $count = reset($data);

            $进程数 = ceil($count / NewsCount_PerProcess);
            for ($i = 0; $i <= $进程数; $i++) {
                $limit = $i * NewsCount_PerProcess;
                $command = "nohup php index.php cli $type $id $limit >> news.log &";
                exec($command);
                logger()->logInfo("群发消息命令：" . $command);
            }
        }
    }
}